﻿using System;
using System.Collections.Generic;

using H2.NET.Business;

namespace H2.NET.Web.Pages
{
    [Serializable]
    public class MDCTabStatus
    {
        #region 属性 Public
        public string KeyWord { get { return _KeyWord; } set { _KeyWord = value; } }
        private string _KeyWord = "";

        public string FORM { get { return _FORM; } set { _FORM = value; } }
        private string _FORM = "";

        public string Category { get { return _Category; } set { _Category = value; } }
        private string _Category = "";

        public string Name { get { return _Name; } set { _Name = value; } }
        private string _Name = "";
        
        public bool IsMultiple { get { return _IsMultiple; } set { _IsMultiple = value; } }
        private bool _IsMultiple = false;

        public bool IsMultipleEditable { get { return _IsMultipleEditable; } set { _IsMultipleEditable = value; } }
        private bool _IsMultipleEditable = false;
        
        public bool IsUsed { get { return _IsUsed; } set { _IsUsed = value; } }
        private bool _IsUsed = false;

        public bool IsEdited { get { return _IsEdited; } set { _IsEdited = value; } }
        private bool _IsEdited = false;

        public string SAP_Code { get { return _SAP_Code; } set { _SAP_Code = value; } }
        private string _SAP_Code = "";

        public string Technical { get { return _Technical; } set { _Technical = value; } }
        private string _Technical = "";

        public string Marketing { get { return _Marketing; } set { _Marketing = value; } }
        private string _Marketing = "";

        public string TradeMarketing { get { return _TradeMarketing; } set { _TradeMarketing = value; } }
        private string _TradeMarketing = "";

        public string Planning { get { return _Planning; } set { _Planning = value; } }
        private string _Planning = "";

        public string Finance { get { return _Finance; } set { _Finance = value; } }
        private string _Finance = "";

        public string Logistics { get { return _Logistics; } set { _Logistics = value; } }
        private string _Logistics = "";

        public string CA { get { return _CA; } set { _CA = value; } }
        private string _CA = "";

        public string Purchasing { get { return _Purchasing; } set { _Purchasing = value; } }
        private string _Purchasing = "";

        public string DMO { get { return _DMO; } set { _DMO = value; } }
        private string _DMO = "";

        public string Package { get { return _Package; } set { _Package = value; } }
        private string _Package = "";

        public string QA { get { return _QA; } set { _QA = value; } }
        private string _QA = "";

        public string Production { get { return _Production; } set { _Production = value; } }
        private string _Production = "";

        public string BD { get { return _BD; } set { _BD = value; } }
        private string _BD = "";

        public string TWMarketing { get { return _TWMarketing; } set { _TWMarketing = value; } }
        private string _TWMarketing = "";

        public string TWSales { get { return _TWSales; } set { _TWSales = value; } }
        private string _TWSales = "";

        public string TWTechnical { get { return _TWTechnical; } set { _TWTechnical = value; } }
        private string _TWTechnical = "";
        public string TWPurchasing { get { return _TWPurchasing; } set { _TWPurchasing = value; } }
        private string _TWPurchasing = "";
        public string TWSupplyChain { get { return _TWSupplyChain; } set { _TWSupplyChain = value; } }
        private string _TWSupplyChain = "";
        public string TWWarehouse { get { return _TWWarehouse; } set { _TWWarehouse = value; } }
        private string _TWWarehouse = "";
        public string CCC { get { return _CCC; } set { _CCC = value; } }
        private string _CCC = "";
        public string TWPlanning { get { return _TWPlanning; } set { _TWPlanning = value; } }
        private string _TWPlanning = "";
        public string TWFinance { get { return _TWFinance; } set { _TWFinance = value; } }
        private string _TWFinance = "";
        public string TWNBD { get { return _TWNBD; } set { _TWNBD = value; } }
        private string _TWNBD = "";
        // 0, Form
        // 1, Category
        // 2, 1*M
        // 3, Technical
        // 4, Marketing
        // 5, TradeMarketing
        // 6, Planning
        // 7, Finance
        // 8, Logistics
        // 9, CA	
        //10, Purchasing	
        //11, DMO	
        //12, QA	
        #endregion

        #region blank
        public MDCTabStatus()
        {

        }
        #endregion

        #region from ss
        public MDCTabStatus(TaskAPI api, string variable)
        {
            try
            {
                _KeyWord = (variable == null ? "" : variable);

                object[] ss = ((object[])api[variable]);
                _FORM = ss.Length >= 1 && ss[0] != null ? ss[0].ToString().ToUpper() : "";

                if (string.IsNullOrEmpty(_FORM))
                {
                    return;
                }

                //拆解电子表格里的配置项
                _Category       = ss.Length >= 2  && ss[1] != null ? ss[1].ToString().ToUpper() : "";

                string[] arrMultiple = new string[10];
                if(ss.Length >= 3  && ss[2] != null)
                {
                    arrMultiple = ss[2].ToString().ToUpper().Split('-');
                }

                _IsMultiple         = arrMultiple.Length >=1 && arrMultiple[0] == "M";
                _IsMultipleEditable = arrMultiple.Length >=2 && arrMultiple[1] == "C";

                _Technical      = ss.Length >= 4  && ss[3] != null ? ss[3].ToString().ToUpper() : "";
                _Marketing      = ss.Length >= 5  && ss[4] != null ? ss[4].ToString().ToUpper() : "";
                _TradeMarketing = ss.Length >= 6  && ss[5] != null ? ss[5].ToString().ToUpper() : "";
                _Planning       = ss.Length >= 7  && ss[6] != null ? ss[6].ToString().ToUpper() : "";
                _Finance        = ss.Length >= 8  && ss[7] != null ? ss[7].ToString().ToUpper() : "";
                _Logistics      = ss.Length >= 9  && ss[8] != null ? ss[8].ToString().ToUpper() : "";
                _CA             = ss.Length >= 10 && ss[9] != null ? ss[9].ToString().ToUpper() : "";
                _Purchasing     = ss.Length >= 11 && ss[10] != null ? ss[10].ToString().ToUpper() : "";
                _DMO            = ss.Length >= 12 && ss[11] != null ? ss[11].ToString().ToUpper() : "";
                _QA             = ss.Length >= 13 && ss[12] != null ? ss[12].ToString().ToUpper() : "";
                _Package        = ss.Length >= 14 && ss[13] != null ? ss[13].ToString().ToUpper() : "";
                _Production     = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _BD             = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _CCC            = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWMarketing    = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWSales        = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWTechnical    = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWPurchasing   = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWSupplyChain  = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWWarehouse    = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWPlanning     = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWFinance = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _TWNBD = ss.Length >= 15 && ss[14] != null ? ss[14].ToString().ToUpper() : "";
                _IsUsed = !(string.IsNullOrEmpty(_Technical) && string.IsNullOrEmpty(_Marketing)
                                                             && string.IsNullOrEmpty(_TradeMarketing)
                                                             && string.IsNullOrEmpty(_Planning)
                                                             && string.IsNullOrEmpty(_Finance)
                                                             && string.IsNullOrEmpty(_Logistics)
                                                             && string.IsNullOrEmpty(_CA)
                                                             && string.IsNullOrEmpty(_Purchasing)
                                                             && string.IsNullOrEmpty(_DMO)
                                                             && string.IsNullOrEmpty(_QA)
                                                             && string.IsNullOrEmpty(_Package)
                                                             && string.IsNullOrEmpty(_BD)
                                                             && string.IsNullOrEmpty(_Production)
                                                             && string.IsNullOrEmpty(_CCC)
                                                             && string.IsNullOrEmpty(_TWMarketing)
                                                             && string.IsNullOrEmpty(_TWSales)
                                                             && string.IsNullOrEmpty(_TWPurchasing)
                                                             && string.IsNullOrEmpty(_TWSupplyChain)
                                                             && string.IsNullOrEmpty(_TWWarehouse)
                                                             && string.IsNullOrEmpty(_TWTechnical)
                                                             && string.IsNullOrEmpty(_TWPlanning)
                                                             && string.IsNullOrEmpty(_TWFinance)
                                                             && string.IsNullOrEmpty(_TWNBD));

                _IsEdited = _Technical == "C" || _Marketing == "C" || _TradeMarketing == "C" || _Planning == "C"
                                              || _Finance == "C" || _Logistics == "C" || _CA == "C" || _Purchasing == "C" || _DMO == "C"
                                              || _QA == "C" || _Package == "C" || _Production == "C";

            }
            catch (Exception)
            {
                TaskDB.Log("", false, "“" + (variable == null ? "" : variable) + "”对应的电子表格变量不存在！");
            }
        }
        #endregion

        #region from database
        public MDCTabStatus(IList<mdc_Config> config, string keyword)
        {
            try
            {
                _KeyWord = keyword;

                mdc_Config conf = new mdc_Config();
                foreach (mdc_Config c in config)
                {
                    if (c.KeyWord == keyword)
                    {
                        conf = c;
                        break;
                    }
                }

                //如果值不存在，退出
                if (conf == null || string.IsNullOrEmpty(conf.KeyWord))
                {
                    return;
                }

                _FORM                = conf.Form.ToUpper();
                _Category            = conf.Category.ToUpper();
                _Name                = conf.Name;
                string[] arrMultiple = conf.Multiple == null ? new string[2]{"",""} : conf.Multiple.ToUpper().Split('-');
                _IsMultiple          = arrMultiple.Length >=1 && arrMultiple[0] == "M";
                _IsMultipleEditable  = arrMultiple.Length >=2 && arrMultiple[1] == "C";

                _SAP_Code       = conf.SAP_Code.ToString().ToUpper();
                _Technical      = conf.Technical.ToString().ToUpper();
                _Marketing      = conf.Marketing.ToString().ToUpper();
                _TradeMarketing = conf.TMarketing.ToString().ToUpper();
                _Planning       = conf.Planning.ToString().ToUpper();
                _Finance        = conf.Finance.ToString().ToUpper();
                _Logistics      = conf.Logistics.ToString().ToUpper();
                _CA             = conf.CA.ToString().ToUpper();
                _Purchasing     = conf.Purchasing.ToString().ToUpper();
                _DMO            = conf.DMO.ToString().ToUpper();
                _QA             = conf.QA.ToString().ToUpper();
                _BD             = conf.QA.ToString().ToUpper();
                _CCC            = conf.Technical.ToString().ToUpper();
                _TWMarketing    = conf.Marketing.ToString().ToUpper();
                _TWSales        = conf.Marketing.ToString().ToUpper();
                _TWTechnical    = conf.Technical.ToString().ToUpper();
                _TWPurchasing   = conf.Technical.ToString().ToUpper();
                _TWSupplyChain  = conf.Logistics.ToString().ToUpper();
                _TWWarehouse    = conf.Logistics.ToString().ToUpper();
                _TWPlanning     = conf.Planning.ToString().ToUpper();
                _TWFinance = conf.Planning.ToString().ToUpper();
                _TWNBD = conf.Planning.ToString().ToUpper();
                _IsUsed = !(string.IsNullOrEmpty(_Technical) && string.IsNullOrEmpty(_Technical) 
                                                             && string.IsNullOrEmpty(_Marketing)
                                                             && string.IsNullOrEmpty(_TradeMarketing)
                                                             && string.IsNullOrEmpty(_Planning)
                                                             && string.IsNullOrEmpty(_Finance)
                                                             && string.IsNullOrEmpty(_Logistics)
                                                             && string.IsNullOrEmpty(_CA)
                                                             && string.IsNullOrEmpty(_Purchasing)
                                                             && string.IsNullOrEmpty(_DMO)
                                                             && string.IsNullOrEmpty(_QA)
                                                             && string.IsNullOrEmpty(_BD)
                                                             && string.IsNullOrEmpty(_CCC)
                                                             && string.IsNullOrEmpty(_TWMarketing)
                                                             && string.IsNullOrEmpty(_TWSales)
                                                             && string.IsNullOrEmpty(_TWTechnical)
                                                             && string.IsNullOrEmpty(_TWPurchasing)
                                                             && string.IsNullOrEmpty(_TWSupplyChain)
                                                             && string.IsNullOrEmpty(_TWWarehouse)
                                                             && string.IsNullOrEmpty(_TWPlanning)
                                                             && string.IsNullOrEmpty(_TWFinance)
                                                             && string.IsNullOrEmpty(_TWNBD)
                                                             && string.IsNullOrEmpty(_SAP_Code));
                                                             

                _IsEdited = _Technical == "C" || _Marketing == "C" || _TradeMarketing == "C" || _Planning == "C" || _Finance == "C" || _Logistics == "C" || _CA == "C" || _Purchasing == "C" || _DMO == "C" || _QA == "C" || _SAP_Code == "C"
                         || _Technical == "E" || _Marketing == "E" || _TradeMarketing == "E" || _Planning == "E" || _Finance == "E" || _Logistics == "E" || _CA == "E" || _Purchasing == "E" || _DMO == "E" || _QA == "E" || _SAP_Code == "E";
            }                                                                                                                                                                                                            
            catch (Exception)
            {
                TaskDB.Log("", false, "“" + keyword + "”对应的数据库配置记录不存在！");
            }
        }
        #endregion
    }
}